/*
 * SetPeriod.java
 *
 * Created on July 14, 2006, 8:46 AM
 */

package org.vcboard.view;

import java.text.*;
import java.util.*;
import javax.swing.*;
import org.vcboard.*;
import org.vcboard.database.*;
import org.vcboard.model.*;

/**
 * @author  Josh
 */
public class SetPeriod extends javax.swing.JDialog
{
   /** A return status code - returned if Cancel button has been pressed */
   public static final int RET_CANCEL = 0;
   /** A return status code - returned if OK button has been pressed */
   public static final int RET_OK = 1;
   
   private static SpinnerNumberModel daySpinnerModel = new SpinnerNumberModel(1, 1, 31, 1);
   private static SpinnerNumberModel lengthSpinnerModel = new SpinnerNumberModel(1, 1, 200, 1);
   
   private static final String[] monthNames = 
   {
      "January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
   };
   
   private static User curUser = null;
   private static LogDate startLog = new LogDate();
   
   /** Creates new form SetPeriod */
   public SetPeriod(java.awt.Frame parent, boolean modal)
   {
      super(parent, modal);
      initComponents();
      
      WorkTimer.centerForm(this);
      
      periodBeginDateSpinner.setModel(daySpinnerModel);
      insertMonths(periodBeginMonthCombo);
      insertYears(periodBeginYearCombo);
      periodBeginYearCombo.setSelectedItem(getYear());
      periodLengthSpinner.setModel(lengthSpinnerModel);
   }
   
   private void insertMonths(JComboBox box)
   {
      for (int i = 0; i < monthNames.length; i++)
      {
         box.addItem(monthNames[i]);
      }
   }
   
   private void insertYears(JComboBox box)
   {  
      int thisYear = getYear();
      for (int i = thisYear - 5; i < thisYear + 5; i++)
      {
         box.addItem(i);
      }
   }
   
   public int getYear()
   {
      Date now = new Date();
      Format format = new SimpleDateFormat("yyyy");
      return Integer.parseInt(format.format(now));
   }
   
   public void setModel(User aUser)
   {
      curUser = aUser;
      startLog = new LogDate(curUser.getStart());
      
      // Set the field values
      if (startLog.getTimeInSeconds() > 0)
      {
         periodBeginDateSpinner.setValue(startLog.getDay());
         periodBeginMonthCombo.setSelectedIndex(startLog.getMonth() - 1);
         periodBeginYearCombo.setSelectedItem(startLog.getYear());
      }
      else
      {
         // Defaults to today
         Calendar now = Calendar.getInstance();
         Format df = new SimpleDateFormat("dd");
         Format mf = new SimpleDateFormat("MMMMMMMMMM");
         Format yf = new SimpleDateFormat("yyyy");
         
         String day = df.format(now.getTimeInMillis());
         String month = mf.format(now.getTimeInMillis());
         String year = yf.format(now.getTimeInMillis());
         
         periodBeginDateSpinner.setValue(Integer.parseInt(day));
         periodBeginMonthCombo.setSelectedItem(month);
         periodBeginYearCombo.setSelectedItem(year);
      }

      periodLengthSpinner.setValue(curUser.getPeriod() / 86400);
   }
   
   public User getModel()
   {
      return curUser;
   }
   
   /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
   public int getReturnStatus()
   {
      return returnStatus;
   }
   
   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
   private void initComponents()
   {
      okButton = new javax.swing.JButton();
      cancelButton = new javax.swing.JButton();
      jLabel1 = new javax.swing.JLabel();
      periodBeginDateSpinner = new javax.swing.JSpinner();
      periodBeginMonthCombo = new javax.swing.JComboBox();
      periodBeginYearCombo = new javax.swing.JComboBox();
      jLabel2 = new javax.swing.JLabel();
      periodLengthSpinner = new javax.swing.JSpinner();
      jSeparator1 = new javax.swing.JSeparator();
      jLabel3 = new javax.swing.JLabel();

      setTitle("Set Period");
      setAlwaysOnTop(true);
      setModal(true);
      setResizable(false);
      addWindowListener(new java.awt.event.WindowAdapter()
      {
         public void windowClosing(java.awt.event.WindowEvent evt)
         {
            closeDialog(evt);
         }
      });

      okButton.setText("OK");
      okButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            okButtonActionPerformed(evt);
         }
      });

      cancelButton.setText("Cancel");
      cancelButton.addActionListener(new java.awt.event.ActionListener()
      {
         public void actionPerformed(java.awt.event.ActionEvent evt)
         {
            cancelButtonActionPerformed(evt);
         }
      });

      jLabel1.setText("Period Begins:");

      jLabel2.setText("Pay Period:");

      jLabel3.setText("days");

      org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
               .add(layout.createSequentialGroup()
                  .addContainerGap()
                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                     .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE))
                     .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .add(6, 6, 6)
                        .add(periodLengthSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel3))
                     .add(layout.createSequentialGroup()
                        .add(periodBeginDateSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 42, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(periodBeginMonthCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 121, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(periodBeginYearCombo, 0, 73, Short.MAX_VALUE))))
               .add(layout.createSequentialGroup()
                  .add(54, 54, 54)
                  .add(okButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                  .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)))
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
         .add(layout.createSequentialGroup()
            .addContainerGap()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
               .add(jLabel1)
               .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
               .add(periodBeginDateSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(periodBeginMonthCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(periodBeginYearCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
               .add(jLabel2)
               .add(periodLengthSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
               .add(jLabel3))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
               .add(okButton)
               .add(cancelButton))
            .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );
      pack();
   }// </editor-fold>//GEN-END:initComponents
   
   private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
   {
      LogDate tmp = new LogDate();
      
      Calendar periodStart = Calendar.getInstance();
      
      periodStart.set(
            Integer.parseInt(periodBeginYearCombo.getSelectedItem().toString()),
            periodBeginMonthCombo.getSelectedIndex(),
            Integer.parseInt(periodBeginDateSpinner.getValue().toString()),
            0, 0, 0);
      tmp.setTimeInMillis(periodStart.getTimeInMillis());
      
      MySQLDbIf.getInstance().setPeriod(tmp, 
            Long.valueOf(periodLengthSpinner.getValue().toString()));
      WorkTimer.getUser().setPeriod(Long.valueOf(periodLengthSpinner.getValue().toString()) * 86400);
      WorkTimer.getUser().setStart(tmp.getTimestamp());
      WorkTimerGUI.getInstance().userChanged();
      
      doClose(RET_OK);
   }//GEN-LAST:event_okButtonActionPerformed
   
   private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
   {
      doClose(RET_CANCEL);
   }//GEN-LAST:event_cancelButtonActionPerformed
   
   /** Closes the dialog */
   private void closeDialog(java.awt.event.WindowEvent evt)//GEN-FIRST:event_closeDialog
   {
      doClose(RET_CANCEL);
   }//GEN-LAST:event_closeDialog
   
   private void doClose(int retStatus)
   {
      returnStatus = retStatus;
      setVisible(false);
      dispose();
   }
   
   /**
    * @param args the command line arguments
    */
   public static void main(String args[])
   {
      java.awt.EventQueue.invokeLater(new Runnable()
      {
         public void run()
         {
            new SetPeriod(new javax.swing.JFrame(), true).setVisible(true);
         }
      });
   }
   
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton cancelButton;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JSeparator jSeparator1;
   private javax.swing.JButton okButton;
   private javax.swing.JSpinner periodBeginDateSpinner;
   private javax.swing.JComboBox periodBeginMonthCombo;
   private javax.swing.JComboBox periodBeginYearCombo;
   private javax.swing.JSpinner periodLengthSpinner;
   // End of variables declaration//GEN-END:variables
   
   private int returnStatus = RET_CANCEL;
}
